<?php
class PageList extends DBList {
	protected $dbTable = "tblPage";
	protected $rowClassType = "PageItem";
}

class PageItem extends DBListItem {
	
	/**
	 * PageItem of the ParentPage if exists
	 * @var PageItem
	 */
	private $parentPage;
	
	
	/**
	 * List of SubPages identified by fkParent
	 * @var PageList
	 */
	private $subPages;
	
	/**
	 * Stores the Level of Page inside the Navigation
	 * @var int
	 */
	private $navigationLevel;
	
	/*
	 * Area to wich this Page belongs to
	 * @var AreaListItem
	 */
	private $area;
	
	/**
	 * The PageSet wich defines the Pages around the current Page
	 * @var NestedPageSet
	 */
	private $nestedPageSet;
	
	/**
	 * Object for the PageLayout
	 * @var PageLayoutItem
	 */
	private $pageLayout;
	
	/**
	 * Name of the Design
	 * @var unknown_type
	 */
	private $designName;
	

	protected function loadCustomData() {
		
		$this->lazydata["ModuleInstance"] = "getModuleInstance";
	}
	
	public function getParentPage() {

		if(!isset($this->parentPage) || $this->parentPage==null) {
			
			// Is there a Parent Page, probably we're the top Page direct under the Area
			if($this->fkParentPage) {
				//echo "Load parentpage for $this->pageTitle\n";
				$this->parentPage = new PageItem($this->fkParentPage);
			}
			else {
				//echo "fkParentPage not set for $this->pageTitle\n";
			}
			
		}
		else {
			//echo "Don't Load parentpage again for $this->pageTitle\n";
		}

		return $this->parentPage;
	}
	
	/**
	 * Sets the ParentPage for the Item. Used when a Page loads it subpages, the Page can set itself as ParentPage
	 * @param $parent
	 * @return unknown_type
	 */
	protected function setParentPage($parent) {
		$this->parentPage = $parent;
	}
	
	
	public function getSubPages() {
		
		if(!isset($this->subPages) || $this->subPages==null) {
			
			$list = new PageList();
			$list->getByFkParentPage($this->pkPage);
			$this->subPages = $list;
			
			foreach ($list as $page) {
				$page->setParentPage($this);
			}
			
		}
		
		return $this->subPages;
		
	}
	
	public function getNavigationLevel() {
		
		//Return the level of the current Item, 1 means top, 
		if(!isset($this->navigationLevel) || $this->navigationLevel==null) {

			//Test how many times we can get a parent Page
			$level = 0;
			$parent = $this->getParentPage();
			while(isset($parent)) {
				
				$level++;
				$parent = $parent->getParentPage();
			}
			
			$this->navigationLevel = $level;
			
		}
		
		return $this->navigationLevel;
		
	}
	
	public function getModuleInstance() {
		if($this->fkModuleInstance)
			return new ModuleInstanceItem($this->fkModuleInstance);
	}
	
	public function getFkArea() {

		//Load design from Area
		$fkArea = $this->fkArea;
		
		if(!$fkArea) {
			$fkArea = $this->getParentPage()->fkArea;
		}

		return $fkArea;
	}
	
	public function getArea() {
		return new AreaItem($this->getFkArea());
	}
	
	public function getDesignMode() {

	}
	
	public function getDesignName() {
		
		if(!isset($this->designName)) {
			$this->designName = $this->getArea()->Design->sysname;
		}
		return $this->designName;
	}
	
	public function getNestedPageMode() {
		
		if(!isset($this->nestedPageSet)) {
			$this->loadNestedPageSet();
		}

		return $this->nestedPageMode;
	}
	
	public function getNestedPageSet() {

		if(!isset($this->nestedPageSet)) {
			$this->loadNestedPageSet();
		}
		
		return $this->nestedPageSet;
		
	}
	
	/**
	 * Is used in Admin mode, to not have any nested pages for this current page
	 * @return void
	 */
	public function disableNestedPages() {
		
		$this->nestedPageMode = "Own";
		$this->NestedPageSet = array();
		
	}
	
	public function getFkPageLayout() {
		if(!$this->fkPageLayout) {
			if(isset($this->parentPage)) {
				//Get From Parent Page
				$this->fkPageLayout = $this->parentPage->getFkPageLayout();
			} 
			else {
				// Get From Area
				$this->fkPageLayout = $this->getArea()->fkPageLayout;
			}
		}
		
		return $this->fkPageLayout;
	}
	
	public function getPageLayout() {
		
		if(!isset($this->pageLayout)) {
			$this->pageLayout = new PageLayoutItem($this->getFkPageLayout());
		}
		
		return $this->pageLayout;
	}
	
	public function getPageLayoutName() {
		
		return $this->getPageLayout()->sysname;
	}
	
	private function loadNestedPageSet() {
		
		//echo "loading NestedPageSet for Page id=".$this->pkPage;
		
		$npsl = new NestedPageSetList();
		
		//Load NestedPageset
		if(!$this->nestedPageMode) {

			//Load Parent or Area
			if(!($this->getParentPage())) {
				//Get NestedPageSetting from Area
				$this->fkNestedPageSet = $this->getArea()->fkNestedPageSet;
			}
			else {
				//Get NestedPage From ParentPage
				//$this->fkNestedPageSet = $this->getParentPage()->getFkNestedPageSet;
				$this->nestedPageSet = $this->getParentPage()->getNestedPageSet();
				//$this->nestedPageMode = "Own";
			}
			
			$list = $npsl->getByPkNestedPageSet($this->fkNestedPageSet);
			if($list->count()) { 
				$this->nestedPageSet = $list->At(0);
				$this->nestedPageMode = "own";
			}
		}
		elseif($this->nestedPageMode=="none") {
			//Clear it, don't have to do more	
		}
		elseif($this->nestedPageMode=="own") {
			//Load new NestedPageSet
			$list = $npsl->getByPkNestedPageSet($this->fkNestedPageSet);
			$this->nestedPageSet = $list->At(0);
			
		}
		elseif($this->nestedPageMode=="include") {
			$list = $npsl->getByPkNestedPageSet($this->fkNestedPageSet);
			$this->nestedPageSet = $list->At(0);
			
		}
		elseif($this->nestedPageMode=="merge") {
			//Merge if we're not the root
			
			
			if (!isset($IsRootPage) || !$IsRootPage) {
				$list = $npsl->getByPkNestedPageSet($this->fkNestedPageSet);
				$this->nestedPageSet = $list->At(0);
				$pnps = $this->parentPage->getNestedPageSet();
				$this->nestedPageSet->merge($pnps);
				
				$this->nestedPageMode="own";
				
			}
			else {
				// Cant do this, are you crazy, ever merged without any on top?
				// Yes, you are crazy, so set the mode to own if an nestedPage exists on this current page and an 
			}
			
		}
			
	}	
}